<?php
namespace Admin\Model;
class AdminModel extends AdminBaseModel
{
    /**
     * 登录
     * @param $uname
     * @param $upass
     * @return array
     */
    public function login($uname, $upass)
    {

        $where['uname'] = $uname;
        $userInfo = $this->where($where)->find();
        if ($userInfo) {
            if ($userInfo['status'] == 0) {
                return ['status' => 0, 'info' => '账号已被冻结'];
            }
            if (password_verify($upass, $userInfo['upass'])) {
                $this->_setLoginInfo($userInfo);
                return ['status' => 1, 'info' => '登录成功'];
            } else {
                return ['status' => 0, 'info' => '用户或密码不正确'];
            }
        } else {
            return ['status' => 0, 'info' => '用户名不存在'];
        }
    }

    /**
     * 设置登录信息
     * @param $userInfo
     */
    protected function _setLoginInfo($userInfo)
    {
        $info['uid'] = $userInfo['id'];
        $info['uname'] = $userInfo['uname'];
        $info['unick'] = $userInfo['unick'];
        $info['role_id'] = $userInfo['roleid'];
        session('user', $info);
    }

    public function logout()
    {

        session('user', null);
        session('[destroy]');
    }

    public function setPass($where, $upass)
    {

        $data['upass'] = password_hash($upass, PASSWORD_DEFAULT);
        return $this->where($where)->save($data);
    }
}